package com.example.mylibrary.util;

import com.example.mylibrary.data.StudentInfo;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;



/**
 * Created by Administrator on 2018/3/22.
 */

public class ExportExcelUtli {

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
    //获取当前时间

    Date date = new Date(System.currentTimeMillis());
    String filename = "/mnt/sdcard/FP-KTA2203/Excel"+simpleDateFormat.format(date)+".xls" ;
    public  ExportExcelUtli(List<StudentInfo> listInfos) {
        Workbook wb = new HSSFWorkbook();
        Sheet sh = wb.createSheet();
        Row row = sh.createRow(0);
        CellStyle style = wb.createCellStyle();
        //设置居中
        style.setAlignment(CellStyle.ALIGN_CENTER);

        Cell cell = row.createCell((short) 0);
        cell.setCellValue("编号");
        cell.setCellStyle(style);
        cell = row.createCell((short) 1);
        cell.setCellValue("考号");
        cell.setCellStyle(style);
        cell = row.createCell((short) 2);
        cell.setCellValue("姓名");
        cell.setCellStyle(style);
        cell = row.createCell((short) 3);
        cell.setCellValue("性别");
        cell.setCellStyle(style);
        cell = row.createCell((short) 4);
        cell.setCellValue("单位");
        cell.setCellStyle(style);
        cell = row.createCell((short) 5);
        cell.setCellValue("头像状态");
        cell.setCellStyle(style);
        cell = row.createCell((short) 6);
        cell.setCellValue("检录状态");
        cell.setCellStyle(style);
        cell = row.createCell((short) 7);
        cell.setCellValue("检录时间");
        cell.setCellStyle(style);
        cell = row.createCell((short) 8);
        cell.setCellValue("发送状态");
        cell.setCellStyle(style);
        cell = row.createCell((short) 9);
        cell.setCellValue("发送时间");
        cell.setCellStyle(style);



        for(int i=0;i<listInfos.size();i++){

           StudentInfo stus =   listInfos.get(i);
            row = sh.createRow(i+1);
            row.createCell((short) 0).setCellValue(i+1);
            row.createCell((short) 1).setCellValue(stus.getStudenCode());
            row.createCell((short) 2).setCellValue(stus.getStudentName());
            row.createCell((short) 3).setCellValue(stus.getSex());
            row.createCell((short) 4).setCellValue(stus.getSchool());
//            if (stus.getTextStateHead()!=null){
//                row.createCell((short) 5).setCellValue("有数据");
//            }else{
//                row.createCell((short) 5).setCellValue("无数据");
//            }
            row.createCell((short) 6).setCellValue(stus.getRollCallState());
            row.createCell((short) 7).setCellValue(stus.getFinalRollCalltime());
            row.createCell((short) 8).setCellValue(stus.getUploadState());
            row.createCell((short) 9).setCellValue(stus.getUploadTime());

        }

        try {

            FileOutputStream fos = new FileOutputStream (filename);
            wb.write(fos);
            fos.close();


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
